﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Reflection;
using System.Security.Policy;
using System.Reflection.Emit;


namespace 玩具商城管理系统
{
    public partial class Tshopcar : Form
    {
        public static string Commodityid = "";
        public Tshopcar()
        {
            InitializeComponent();
        }
        public static decimal profit = 0;
        SqlDataAdapter dashopcar, daProduct;
        DataSet ds=new DataSet();

        Class1 shopcarSrv = new Class1();
        void init()
        {
            string str = "select shopcar_id,shopcar_commodityid,shopcar_clientid,shopcar_commodityname,shopcar_money,shopcar_num from shopcar where shopcar_clientid='"+login.username+"'";
            dashopcar = new SqlDataAdapter(str, DB.cn);
            dashopcar.Fill(ds, "shopcar_info");
            string sdr = "select * from commodity";
            daProduct = new SqlDataAdapter(sdr, DB.cn);
            daProduct.Fill(ds, "product_info");
        }
        void show()
        {
            DataGridViewButtonColumn bcCode = new DataGridViewButtonColumn();
            bcCode.Name = "bcCode";
            bcCode.HeaderText = "详细信息";
            bcCode.Text = "详细信息";
            bcCode.UseColumnTextForButtonValue = true;  
            dgvProduct.Columns.Add(bcCode);
            DataGridViewCheckBoxColumn acCode = new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode";
            acCode.HeaderText = "选择";
            dgvProduct.Columns.Add(acCode);
            
        }
        void dgvRow()
        {
            dgvProduct.Columns[0].HeaderText = "购物车编号";
            dgvProduct.Columns[1].HeaderText = "客户名称";
            dgvProduct.Columns[2].HeaderText = "商品编号";
            dgvProduct.Columns[3].HeaderText = "商品名称";
            dgvProduct.Columns[4].HeaderText = "商品价格";
            dgvProduct.Columns[5].HeaderText = "商品数量";
        }

        private void dgvProduct_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            int CIndex = e.ColumnIndex;
            if (CIndex == 6)
            {
                int rowindex = e.RowIndex;
                Commodityid = dgvProduct.Rows[rowindex].Cells[0].Value.ToString();
                Tcommodity tl = new Tcommodity();
                tl.ShowDialog();
            }
            if (dgvProduct.Rows.Count > 0)
            {
                for(int i=0; i<dgvProduct.Rows.Count; i++){
                    DataGridViewCheckBoxCell ck = dgvProduct.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;
                    if (i != e.RowIndex)
                    {
                        ck.Value = false;
                    }
                    else
                    {
                        ck.Value= true;
                    }
                }

            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        void Bind()
        {
            init();
            DB.GetCn();
            string sdr = "select client_power from client where client_name='" + login.username + "'";
            SqlCommand command = new SqlCommand(sdr, DB.cn);
            // 执行查询并获取结果
            object result = command.ExecuteScalar();
            string client_Power = (result != null) ? result.ToString() : "";
            if (client_Power == "高级用户")
            {
                decimal totalPrice = shopcarSrv.GetTotalPriceByshopcar_clientid(login.username).Item1;
                double discountedPrice = (double)totalPrice * 0.8;
                label2.Text = discountedPrice.ToString();
                label4.Text = shopcarSrv.GetTotalPriceByshopcar_clientid(login.username).Item1.ToString();
                
            }
            else if (client_Power == "初级用户")
            {
                decimal totalPrice = shopcarSrv.GetTotalPriceByshopcar_clientid(login.username).Item1;
                double discountedPrice = (double)totalPrice * 0.8;
                label2.Text = discountedPrice.ToString();
                label4.Text = shopcarSrv.GetTotalPriceByshopcar_clientid(login.username).Item1.ToString();
            }
            else
            {
                label4.Text = shopcarSrv.GetTotalPriceByshopcar_clientid(login.username).Item1.ToString();
                label2.Visible= false;
                label3.Visible= false;
            }
            
            profit = shopcarSrv.GetTotalPriceByshopcar_clientid(login.username).Item2;
            DataView dvCartItem = new DataView(ds.Tables["shopcar_info"]);
            dgvProduct.DataSource = dvCartItem;
            dgvRow();
            show();
        }
        void Clear_shopcar()//清空购物车
        {
            List<int> List_index = new List<int>();
            if(dgvProduct.Rows.Count > 0)
            {
                for(int i = 0; i < dgvProduct.Rows.Count-1; i++)
                {
                    List_index.Add(i);

                }
                int z = 0;
                foreach(int n in List_index)
                {
                    dgvProduct.Rows.RemoveAt(n-z);
                    z++;
                }
                UpdateDB();
            }
        }
        void dgvstyle()//datagridview样式
        {
            dgvProduct.Columns[0].Width = 100;
            dgvProduct.Columns[1].Width = 90;
            dgvProduct.Columns[2].Width = 80;
            dgvProduct.Columns[3].Width = 80;
            dgvProduct.Columns[4].Width = 80;
            dgvProduct.Columns[5].Width = 80;
            dgvProduct.Columns[6].Width = 70;
            dgvProduct.Columns[7].Width = 60;
            dgvProduct.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvProduct.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }

        private void button3_Click(object sender, EventArgs e)//选择删除商品
        {
            int s = dgvProduct.Rows.Count;
            for (int i = 0; i < dgvProduct.Rows.Count; i++)
            {
                if (dgvProduct.Rows[i].Cells["acCode"].EditedFormattedValue.ToString()=="True")
                {
                    dgvProduct.Rows.RemoveAt(i);
                    UpdateDB();
                }
                else
                {
                    s = s - 1;
                }
                if (s == 0)
                {
                    MessageBox.Show("请选择要删除的商品");
                }
            }
        }
        void UpdateDB()
        {
            try
            {
                SqlCommandBuilder dbshopcar = new SqlCommandBuilder(dashopcar);
                dashopcar.Update(ds, "shopcar_info");
                SqlCommandBuilder dbProduct = new SqlCommandBuilder(daProduct);
                daProduct.Update(ds, "product_info");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)//重新计算总价
        {
            DB.GetCn();
            string sdr = "select client_power from client where client_name='" + login.username + "'";
            SqlCommand command = new SqlCommand(sdr, DB.cn);
            // 执行查询并获取结果
            object result = command.ExecuteScalar();
            string client_Power = (result != null) ? result.ToString() : "";
            if (client_Power == "高级用户")
            {
                decimal totalPrice = shopcarSrv.GetTotalPriceByshopcar_clientid(login.username).Item1;
                double discountedPrice = (double)totalPrice * 0.8;
                label2.Text = discountedPrice.ToString();
                label4.Text = shopcarSrv.GetTotalPriceByshopcar_clientid(login.username).Item1.ToString();

            }
            else if (client_Power == "初级用户")
            {
                decimal totalPrice = shopcarSrv.GetTotalPriceByshopcar_clientid(login.username).Item1;
                double discountedPrice = (double)totalPrice * 0.8;
                label2.Text = discountedPrice.ToString();
                label4.Text = shopcarSrv.GetTotalPriceByshopcar_clientid(login.username).Item1.ToString();
            }
            else
            {
                label4.Text = shopcarSrv.GetTotalPriceByshopcar_clientid(login.username).Item1.ToString();
                label2.Visible = false;
                label3.Visible = false;
            }
           
            profit = shopcarSrv.GetTotalPriceByshopcar_clientid(login.username).Item2;
        }

        private void button1_Click(object sender, EventArgs e)//清空
        {
            Clear_shopcar();
        }

        private void button4_Click(object sender, EventArgs e)//结算
        {
            if(dgvProduct.Rows.Count==1)
            {
                MessageBox.Show("购物车没有商品请先添加商品到购物车");
            }
            else
            {
                List<int>List_index = new List<int>();
                for (int i = 0; i < dgvProduct.Rows.Count-1; i++)
                {
                    List_index.Add(i);
                }
                foreach(int n in List_index)
                {
                    string str = dgvProduct.Rows[n].Cells["shopcarcommodityid"].Value.ToString();
                        DataRow[] CustRow = ds.Tables["product_info"].Select("commodity_id='"+str+"'");
                        CustRow[0]["commodity_invent"] = (Convert.ToInt32(CustRow[0]["commodity_invent"]) - Convert.ToInt32(dgvProduct.Rows[n].Cells["shopcarnum"].Value)).ToString();
                }
                
                    UpdateDB();
               
                this.Close();
                    Torder ad = new Torder();
                    ad.ShowDialog();
                

            }
            
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void Tshopcar_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“toyStroeDataSet.shopcar”中。您可以根据需要移动或移除它。
            this.shopcarTableAdapter.Fill(this.toyStroeDataSet.shopcar);
            DB.GetCn();

            /*shopcarSrv.Add(login.username, Convert.ToInt32(newwin.Commodityid), 1);*/
            //shopcarSrv.Add(login.username, Convert.ToInt32(Form1.Commodityid), 1);
            Bind();
            dgvstyle();

        }

        
    }
}
